package com.iotimc.attendancesystem.attendance;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.base.BaseFragment;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.manage.ManageActivity;
import com.iotimc.attendancesystem.util.PictureManager;
import com.iotimc.attendancesystem.util.TtsUtil;
import com.makeramen.roundedimageview.RoundedImageView;
import com.pefis.natives.PfsBioInfo;
import com.pefis.view.FaceDetectionView;
import com.pefis.view.FrameView;

import butterknife.BindView;

/**
 * Created by cwj on 2017/11/28.
 */

public class FaceLoginFragment extends BaseFragment implements AttendanceContract.View {

    @BindView(R.id.faceDetectionView)
    FaceDetectionView mFaceDetectionView;
    @BindView(R.id.preview)
    RoundedImageView mPreview;
    @BindView(R.id.frameview)
    FrameView mFrameview;

    AttendanceContract.Presenter presenter;
    int mImageWidth = PfsBioInfo.getInstance().face_img_w[0];
    int mImageHeight = PfsBioInfo.getInstance().face_img_h[0];
    private byte[] mPhotoImage = new byte[mImageWidth * mImageHeight * 4];
    private String mId = "";
    private boolean mStatus;

    @Override
    protected int getResId() {
        return R.layout.fragment_verification_face;
    }

    private boolean add;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        add = true;
    }

    public void setPresenter(AttendanceContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: " + isVisibleToUser);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: " + hidden);
        if (!hidden) {
            PictureManager.getInstance().load(getContext(), R.drawable.test3, mPreview, false);
            presenter.resetCount();
            checkFailCount = 0;
            currentFailTime = 0;
            if (presenter != null) {
                presenter.setCheckAdmin(true);
                presenter.setmFaceDetectionView(this, mFaceDetectionView);
                if (!presenter.isRuning()) {
                    presenter.startAttendance();
                }
            }
        }
    }

    private static final String TAG = "FaceLoginFragment";

    @Override
    public void onResume() {
        super.onResume();
        PictureManager.getInstance().load(getContext(), R.drawable.test3, mPreview, false);

        if (add) {
            add = false;
            onHiddenChanged(add);
        }
        mFaceDetectionView.postInvalidate();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (presenter != null) {
            //presenter.stopAttendance();
        }
    }

    @Override
    public void resetUser() {

    }

    @Override
    public void showImage(Bitmap bitmap) {
        if (getActivity() != null) {
            mPreview.setImageBitmap(bitmap);
        }
    }

    @Override
    public void showUser(final PersonEntity entity, boolean update) {
        if (entity == null) {
            ((SystemActivity) getActivity()).setTime(1);
        } else {
            String entityId = entity.getId();
            if (entity.getAdmin()) {
                mId = entityId;
                //TtsUtil.ttsSpeek("验证成功！");
                ((SystemActivity) getActivity()).showMainFragment();
                ((SystemActivity) getActivity()).stopTime();
                Log.i(TAG, "showUser: checkAdmin");
                Intent intent = new Intent(getActivity(), ManageActivity.class);
                intent.putExtra("admin", true);
                intent.putExtra("code", entity.getCode());
                startActivity(intent);

            } else {
                if (checkFailCount(System.currentTimeMillis())) {
                  //  TtsUtil.ttsSpeek2("验证失败！");
                }
            }
        }
    }

    private int checkFailCount;
    private long currentFailTime;

    private  boolean checkFailCount(long currentTime) {
        if (currentTime - this.currentFailTime > 3000) {
            Log.i(TAG, "accept: checkFailCount" + "=====Fail========" + checkFailCount);
            checkFailCount += 1;
            if (checkFailCount > 3) {
                ((SystemActivity) getActivity()).setTime(1);
                return false;
            } else {
                this.currentFailTime = currentTime;
                return true;
            }
        }
        Log.i(TAG, "accept: checkFailCount" + "======true=======" + checkFailCount);
        return false;
    }
}

